//
//  SQLoginViewController.m
//  energy
//
//  Created by 周曼迪 on 2021/7/6.
//

#import "SQLoginViewController.h"
#import "SQLoginModel.h"
#import "AppDelegate.h"
#import "AFHTTPSessionManager.h"

@interface SQLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *tf_tel;
@property (weak, nonatomic) IBOutlet UIImageView *img_eye;
@property (weak, nonatomic) IBOutlet UITextField *tf_password;
@property (weak, nonatomic) IBOutlet UILabel *lab_versin;

@end

@implementation SQLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.hidNav = YES;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getData) name:@"userInformation" object:nil];
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];

    CFShow((__bridge CFTypeRef)(infoDictionary));

    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [[AFNetworkReachabilityManager sharedManager ] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if(status ==AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi){
            
        }else{
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示网络连接异常" message:@"您尚未授权“龙安_安能保”访问网络的权限，请前往设置开启网络授权" preferredStyle:UIAlertControllerStyleAlert];
    
    
    
               [alertController addAction:[UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    
                   [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    
               }]];
              [self presentViewController:alertController animated:YES completion:nil];
            
        }
    }];
    
   

    self.lab_versin.text = [NSString stringWithFormat:@"%@version %@",[infoDictionary objectForKey:@"CFBundleDisplayName"],[infoDictionary objectForKey:@"CFBundleShortVersionString"]];;
    
}



//༥༤༽ཀབཀྲ་ཤིས་བདེ་ལེགས།



/**登录*/
- (IBAction)clickLogin:(UIButton *)sender {
    [self.view endEditing:YES];
    if ([self verifications]) {
        [SQLoginModel getDataWithParams:@{@"m_phone":self.tf_tel.text,@"m_userpass":self.tf_password.text} WithCallBack:^(SQLoginModel * data) {
            
            //m_id
            [[NSUserDefaults standardUserDefaults] setObject:data.data.m_id forKey:@"MID"];
            
            //m_name
            [[NSUserDefaults standardUserDefaults] setObject:data.data.m_name forKey:@"MName"];
            
            //m_level
            [[NSUserDefaults standardUserDefaults] setObject:data.data.m_level forKey:@"MLevel"];
            
            //m_phone
            [[NSUserDefaults standardUserDefaults] setObject:data.data.m_phone forKey:@"MPhone"];
            
            //m_enterprise_id
            [[NSUserDefaults standardUserDefaults] setObject:data.data.m_enterprise_id forKey:@"MEnterpriseId"];
            
            //m_enterprise_text
            [[NSUserDefaults standardUserDefaults] setObject:data.data.m_enterprise_text forKey:@"MEnterpriseText"];
            
            //m_group 部门中文
            [[NSUserDefaults standardUserDefaults] setObject:data.data.m_group forKey:@"MGroup"];
            
            //m_number 部门id
            [[NSUserDefaults standardUserDefaults] setObject:data.data.m_number forKey:@"MNumber"];
            
            //m_one
            [[NSUserDefaults standardUserDefaults] setObject:data.data.m_one forKey:@"MOne"];
            
            //m_onetext
            [[NSUserDefaults standardUserDefaults] setObject:data.data.m_one_text forKey:@"MOneText"];
            
            //m_two
            [[NSUserDefaults standardUserDefaults] setObject:data.data.m_two forKey:@"MTwo"];
            
            //m_two
            [[NSUserDefaults standardUserDefaults] setObject:data.data.m_two_text forKey:@"MTwoText"];
            
            //m_three
            [[NSUserDefaults standardUserDefaults] setObject:data.data.m_three forKey:@"MThree"];
            //m_three
            [[NSUserDefaults standardUserDefaults] setObject:data.data.m_three_text forKey:@"MThreeText"];

            //m_four
            [[NSUserDefaults standardUserDefaults] setObject:data.data.m_four forKey:@"MFour"];
            //m_four
            [[NSUserDefaults standardUserDefaults] setObject:data.data.m_four_text forKey:@"MFourText"];
            
            //m_code
            [[NSUserDefaults standardUserDefaults] setObject:data.data.m_code forKey:@"MCode"];
            

            
            [[AppDelegate getAppDelegate] jump];
        }];
    }
    
}

#pragma mark ---- 验证
- (BOOL)verifications{
    BOOL ret = NO;
    if([self.tf_tel.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请输入手机号"];
        return NO;
    }  else if(![self.tf_tel.text validatePhoneNum]){
        [CCToast showMessage:@"请输入正确手机号"];
        return NO;
    }
    else if([self.tf_password.text isEqualToString:@""]){
          
        [CCToast showMessage:@"请输入密码"];
           return NO;
    }else{
        ret = YES;
    }
    return YES;
}

#pragma mark ---- 是否显示密码
- (IBAction)clickShow:(UIButton *)sender {
    sender.selected = !sender.isSelected;
     if (sender.selected) {//明文
         [self.img_eye setImage:[UIImage imageNamed:@"keshi.png"]];
         NSString *tempPwdStr = self.tf_password.text;
            self.tf_password.text = @""; // 这句代码可以防止切换的时候光标偏移
            self.tf_password.secureTextEntry = NO;
            self.tf_password.text = tempPwdStr;
     }else{
         [self.img_eye setImage:[UIImage imageNamed:@"bukeshi.png"]];
         NSString *tempPwdStr = self.tf_password.text;
         self.tf_password.text = @"";
         self.tf_password.secureTextEntry = YES;
         self.tf_password.text = tempPwdStr;
     }
    
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
